/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChangePassword.java
 *
 * Created on 05.06.2009, 08:42:08
 */

package dialogs;

import BusinessLayer.BusinessLayer;
import Binding.Binder;
import java.awt.Dimension;
import java.awt.Point;
import logging.Logger;

/**
 *
 * @author benji
 */
public class ChangePassword extends javax.swing.JDialog {

    private Binder binder = new Binder();


    /** Creates new form ChangePassword */
    public ChangePassword(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        getRootPane().setDefaultButton(saveButton);

        Logger.debug("Öffne " + this.getName());

        int x;
        int y;
        Point topLeft = parent.getLocationOnScreen();
        Dimension sizeOfParent = parent.getSize();
        Dimension mySize = getSize();

        if (sizeOfParent.width > mySize.width) {
            x = ((sizeOfParent.width - mySize.width) / 2) + topLeft.x;
        } else {
            x = topLeft.x;
        }

        if (sizeOfParent.height > mySize.height) {
            y = ((sizeOfParent.height - mySize.height) / 2) + topLeft.y;
        } else {
            y = topLeft.y;
        }

        setLocation(x, y);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        oldPasswordField = new javax.swing.JPasswordField();
        newPasswordField = new javax.swing.JPasswordField();
        retryNewPasswordField = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        abbruchButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        errorInfoPanel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(wisi.WiSiApp.class).getContext().getResourceMap(ChangePassword.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        oldPasswordField.setText(resourceMap.getString("oldPasswordField.text")); // NOI18N
        oldPasswordField.setName("oldPasswordField"); // NOI18N

        newPasswordField.setText(resourceMap.getString("newPasswordField.text")); // NOI18N
        newPasswordField.setName("newPasswordField"); // NOI18N

        retryNewPasswordField.setText(resourceMap.getString("retryNewPasswordField.text")); // NOI18N
        retryNewPasswordField.setName("retryNewPasswordField"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        abbruchButton.setText(resourceMap.getString("abbruchButton.text")); // NOI18N
        abbruchButton.setName("abbruchButton"); // NOI18N
        abbruchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                abbruchButtonActionPerformed(evt);
            }
        });

        saveButton.setText(resourceMap.getString("saveButton.text")); // NOI18N
        saveButton.setName("saveButton"); // NOI18N
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        errorInfoPanel.setForeground(resourceMap.getColor("errorInfoPanel.foreground")); // NOI18N
        errorInfoPanel.setText(resourceMap.getString("errorInfoPanel.text")); // NOI18N
        errorInfoPanel.setName("errorInfoPanel"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(errorInfoPanel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 138, Short.MAX_VALUE)
                        .addComponent(saveButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(abbruchButton))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel1)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(retryNewPasswordField)
                            .addComponent(newPasswordField, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(oldPasswordField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(oldPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(newPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(retryNewPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)))
                    .addComponent(jLabel2))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(abbruchButton)
                            .addComponent(saveButton))
                        .addContainerGap(24, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(errorInfoPanel)
                        .addContainerGap())))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * abbrechen es geschiet nichts
     * @param evt
     */
    private void abbruchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abbruchButtonActionPerformed
        this.dispose();
        Logger.debug("Schliese Dialog" + this.getName());
}//GEN-LAST:event_abbruchButtonActionPerformed

    /**
     * passwort ändern
     * @param evt
     */
    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        String oldPassword;
        String newPassword;

        //ist eines der eingabefelder leer??
        if(new String(this.oldPasswordField.getPassword()).isEmpty()){
            //old leer
            this.errorInfoPanel.setText("Bitte Paswort eingeben");
            Logger.debug("kein Passwort eigegeben");
        }
        else if(new String(this.newPasswordField.getPassword()).isEmpty()){
            //new leer
            this.errorInfoPanel.setText("Bitte neues Paswort eingeben");
            Logger.debug("neues Passwort nicht eingegeben");
        }
        else if(new String(this.retryNewPasswordField.getPassword()).isEmpty()){
            //retry leer
            this.errorInfoPanel.setText("Bitte neues Paswort nochmal eingeben eingeben");
            Logger.debug("Keine Wiederholung des Passworts");
        }
        //wenn alle befüllt
        else{
            oldPassword =  new String(this.oldPasswordField.getPassword());
            newPassword =  new String(this.newPasswordField.getPassword());
            //ist das alte passwort korrekt
            if(!BusinessLayer.checkPassword(BusinessLayer.getUser(BusinessLayer.getUserId()), oldPassword)){
                this.errorInfoPanel.setText("Passwort Falsch!");
                Logger.warn("Passwort falsch eingegeben");
            }
            //ist dsa neue 2 mal gleich eingegeben
            else if(!newPassword.equals(new String(this.retryNewPasswordField.getPassword()))){
                this.errorInfoPanel.setText("Die neuen Passwörter Stimmen nicht überein");
                Logger.warn("Neue Passwörter ungleich");
            }
            else{
                //wenn das alte Passwort Stimmt
                //und die 2 neuen übereinstimemn
                //passwort ändern
                BusinessLayer.changePassword(newPassword, BusinessLayer.getUserId());
                Logger.info("Passwort würde geändert werden");
                Logger.trace("Passwort geändert");
                this.dispose();
                Logger.debug("Schliese Dialog " + this.getName());
            }

        }

        
        
    }//GEN-LAST:event_saveButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ChangePassword dialog = new ChangePassword(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton abbruchButton;
    private javax.swing.JLabel errorInfoPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField newPasswordField;
    private javax.swing.JPasswordField oldPasswordField;
    private javax.swing.JPasswordField retryNewPasswordField;
    private javax.swing.JButton saveButton;
    // End of variables declaration//GEN-END:variables

}
